import { fileURLToPath, URL } from 'node:url'

import { defineConfig, splitVendorChunkPlugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import viteCompression from 'vite-plugin-compression'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import vitePluginBundleObfuscator from 'vite-plugin-bundle-obfuscator';

import { VitePWA } from 'vite-plugin-pwa'

const defaultObfuscatorConfig = {
    excludes: ["vue", "element-plus"],
    enable: true,
    log: true,
    autoExcludeNodeModules: true,
    threadPool: true,
    options: {
        compact: true,
        controlFlowFlattening: false, // 是否启用控制流平坦化，开启这个文件会变大很多
        controlFlowFlatteningThreshold: 1,
        deadCodeInjection: false, // 是否启用死代码注入，开启这个文件会变大很多
        debugProtection: true,
        debugProtectionInterval: 0,
        disableConsoleOutput: true,
        identifierNamesGenerator: 'hexadecimal',
        log: false,
        numbersToExpressions: false,
        renameGlobals: false,
        selfDefending: true,
        simplify: true,
        splitStrings: false,
        stringArray: false,
        stringArrayCallsTransform: false,
        stringArrayCallsTransformThreshold: 0.5,
        stringArrayEncoding: ['rc4'],
        stringArrayIndexShift: true,
        stringArrayRotate: true,
        stringArrayShuffle: true,
        stringArrayWrappersCount: 1,
        stringArrayWrappersChainedCalls: true,
        stringArrayWrappersParametersMaxCount: 2,
        stringArrayWrappersType: 'variable',
        stringArrayThreshold: 0.75,
        unicodeEscapeSequence: false, // 是否启用 Unicode 转义序列，开启这个文件会变大很多
    }
};

// https://vite.dev/config/
export default defineConfig({
    build: {
        rollupOptions: {
            output: {
                advancedChunks: {
                    groups: [
                        {
                            test: /node_modules\/vue/,
                            name: 'vue',
                        },
                        {
                            test: /node_modules\/element-plus/,
                            name: 'element-plus',
                        },
                    ],
                },
                experimentalMinChunkSize: 10 * 1024, // 单位b
                chunkFileNames: 'js/[name]-[hash].js',  // 引入文件名的名称
                entryFileNames: 'js/[name]-[hash].js',  // 包的入口文件名称
                assetFileNames: '[ext]/[name]-[hash].[ext]' // 资源文件像 字体，图片等
            },
        },
        esbuild: {
            drop: ['console', 'debugger'] // 在生产环境中删除 console 和 debugger
        },
    },
    plugins: [
        vue(),
        vueDevTools(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver()],
        }),
        viteCompression({
            verbose: true, // 是否在控制台输出压缩结果
            disable: false, // 是否禁用压缩
            threshold: 10240, // 只有大于该值的文件才会被压缩，单位为字节
            algorithm: 'gzip', // 压缩算法
            ext: '.gz', // 压缩后的文件扩展名
            deleteOriginFile: false, // 是否删除原始未压缩的文件
        }),
        vitePluginBundleObfuscator(defaultObfuscatorConfig),
        VitePWA({
            registerType: 'prompt', // 手动触发更新
            includeAssets: ['favicon.ico', 'robots.txt'],
            manifest: {
                name: 'AI-CosPlay',
                short_name: 'AI-CosPlay',
                description: 'AI-CosPlay',
                theme_color: '#409EFF',
                background_color: '#ffffff',
                display: 'standalone',
                start_url: '/',
                icons: [
                    {
                        src: 'SqlToEr-192x192.png',
                        sizes: '192x192',
                        type: 'image/png'
                    },
                    {
                        src: 'SqlToEr-512x512.png',
                        sizes: '512x512',
                        type: 'image/png'
                    }
                ]
            },
            workbox: {
                cleanupOutdatedCaches: true,
                clientsClaim: true
            }
        })
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        },
    },
    server: {
        port: 81,
        open: true,
        proxy: {
            // 配置反向代理
            '/api': {
                target: 'http://localhost:8888', // 后端服务器地址
                changeOrigin: true, // 改变请求源
                rewrite: (path) => path.replace(/^\/api/, '') // 将 /api 前缀去掉
            }
        }
    }
})
